#pragma once

#include "Timer.h"
#include "Epoller.h"
#include "ThreadPool.h"
#include "HTTPConnection.h"

class TCPServer {
private:
    int socket_fd;

    const char *IP;
    uint16_t port;

    static int set_fd_non_block(int fd);

    bool init_socket();

    std::unique_ptr<ThreadPool> thread_pool;              // 线程池
    std::unique_ptr<Timer> timer;                         // 定时器
    std::unique_ptr<Epoller> epoller;                     // epoll对象
    std::unordered_map<int, HTTPConnection> connections;  // 每个客户端和连接对象的映射

public:
    TCPServer(const char *ip, uint16_t port, size_t cores);

    ~TCPServer() = default;

    void read_connection(HTTPConnection *connection, int client_Fd);  // 处理读事件
    void write_connection(HTTPConnection *connection, int client_fd); // 处理写事件
    void disconnection(int client_fd);

    void start();
};